<template>
    <header>
        <!-- 跳转设置 -->
        <li @click="goTosetmeal" class="back-button">
          <img class="icon" :src="'src/img/left.png'" alt="返回图标">
          <div class="user-info" >
        <h3>医生选择</h3>
      </div>
        </li>
    </header>
    <div class="doctor-list">
        <div v-for="doctor in doctors" :key="doctor.docId" class="doctor-item">
          <div class="doctor-details">
            <div class="doctor-info">
              <h3>{{ doctor.realName }}</h3>
              <p>{{ doctor.specialization }}</p>
              <p>{{ doctor.address }}</p>
            </div>
            <button class="consult-button" @click="saveAndGoToConsult(doctor)">去咨询</button>
          </div>
        </div>
      </div>
  </template>
  
  <script>
  import axios from 'axios';
  import { setSessionStorage } from '../common.js'; // 引入存储方法
  
  export default {
    data() {
      return {
        doctors: [],
      };
    },
    mounted() {
      this.fetchDoctors();
    },
    methods: {
      async fetchDoctors() {
        try {
          const response = await axios.post('http://localhost:8080/doctor/list');
          if (response.data.code === 200) {
            this.doctors = response.data.data;
          } else {
            console.error('Failed to fetch doctors:', response.data.msg);
          }
        } catch (error) {
          console.error('Error fetching doctors:', error);
        }
      },
      saveAndGoToConsult(doctor) {
        setSessionStorage('selectedDoctorId', doctor.docId);
        console.log(doctor.docId) // 存储选定医生的 docId 到 sessionStorage
        this.$router.push(`/selectdate?docId=${doctor.docId}`);
      },
      goTosetmeal(){
        this.$router.push('/setmeal')
      }
    },
  };
  </script>
  
  <style scoped>
.user-info {
  position: fixed;
  top: 3%; /* 将元素顶部与视口垂直居中 */
  left: 50%; /* 将元素左侧与视口水平居中 */
  transform: translate(-50%, -50%); /* 使用 transform 属性将元素向左和向上移动自身宽高的一半，以使其在视口中心 */
}

header {
  width: 100%;
  height: 15.7vw;
  background-color: #FFF;

  position: fixed;
  left: 0;
  top: 0;

  display: flex;
  align-items: center;
  justify-content: space-between;

  box-sizing: border-box;
  padding: 0 3.6vw;
}
.doctor-container {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 10vh; /* 100% viewport 高度，确保在整个视口内居中 */
}

  /* 跳转设置 */
.icon{
float: left;

width: 50%;
height: 5.2vw;
}
  .doctor-list {
    display: flex;
    flex-direction: column; /* 列表垂直排列 */
    gap: 20px; /* 项目间距 */
  }
  
  .doctor-item {
    border: 1px solid #ccc;
    padding: 10px;
    background: linear-gradient(to right, #bdd6be, #7be07b);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 20px;
  }
  
  .doctor-details {
    display: flex;
    align-items: center;
  }
  
  .doctor-image {
    width: 100px;
    height: 100px;
    object-fit: cover;
    border-radius: 50%;
    margin-right: 20px;
  }
  
  .doctor-info {
    flex: 1;
  }
  
  .consult-button {
    background-color: green;
    color: white;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
    transition: background-color 0.3s ease;
  }
  
  .consult-button:hover {
    background-color: darkgreen;
  }
  </style>
  